package com.why.blog.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.why.blog.bean.Blog;
import com.why.blog.bean.data;
import com.why.blog.service.BlogService;
import com.why.blog.service.typeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: blog
 * @description: 注意组件命名
 * @author: @why
 * @create: 2021-01-19 12:26
 **/
@Controller
@RequestMapping("/admin")
@Slf4j
public class adminMyBlogController {

    @Autowired
    BlogService blog;

    @Autowired
    typeService type;

    /**
     * 提交数据，跳转成功进入仪表盘
     *
     * @return
     */
    @RequestMapping(value = "/dashboard")
    public String dashboard(Model model) {
        model.addAttribute("blogNums", blog.blogNums());
        model.addAttribute("typeNums", type.typeNums());

        return "admin/dashboard";
    }

    /**
     * 跳转到登录页面
     *
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "admin/login";
    }


    /**
     * 跳转到博客管理页面
     * 以及分页功能
     *
     * @return
     */

    @GetMapping("/blog-mgr")
    public String blogManager(Model model, @RequestParam(defaultValue = "1") Integer pageNum) {
        pageNum = pageNum >= 1 ? pageNum : 1;
        PageHelper.startPage(pageNum, 6);
        PageInfo<Blog> listBlogNoContent = new PageInfo<>(blog.getListBlogNoContent());

        model.addAttribute("blogs", listBlogNoContent);
        return "admin/blog-mgr";
    }

    @ResponseBody
    @GetMapping("/blogMsg")
    public data getBlogMsg() {
        List<Blog> listBlogNoContent = blog.getListBlogNoContent();

        data data = new data();
        data.setData(listBlogNoContent);
        return data;
    }

//    @ResponseBody
    @GetMapping("/deleteBlog")
    public String delelteBlog(@RequestParam Integer id) {
        blog.deleteBlogById(id);
        return "redirect:/admin/blog-mgr";
    }


}
